﻿using System;
using System.Collections.Generic;
using System.Text;
using Asp = System.Web;

namespace Daco.Web.UI.WebControls
{
    public class ULTree : Asp.UI.WebControls.WebControl, Asp.UI.IPostBackDataHandler, Asp.UI.IPostBackEventHandler
    {
        private ITree _dataSource = null;
        public ITree DataSource
        {
            get
            {
                return _dataSource;
            }
            set
            {
                _dataSource = value;
            }
        }

        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                if (!string.IsNullOrEmpty(value) && (string.IsNullOrEmpty(_title) || string.Compare(value, _title, StringComparison.CurrentCultureIgnoreCase) == 0))
                    _title = value;
            }
        }

        public override string ClientID
        {
            get
            {
                return string.Format("PNC_ULTree_{0}", this.UniqueID);
            }
        }

        public ULTree()
            : base(Asp.UI.HtmlTextWriterTag.Div)
        {

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            // Register postback event
            Page.RegisterRequiresPostBack(this);
            // Register event raise
            Page.RegisterRequiresRaiseEvent(this);
        }
        /// <summary>
        /// Render Tree
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.WriteLine(@"<div class=""DIVBox"">
                            <div class=""DIVBoxHeader"">
                                <div><h4>{0}</h4></div>
                            </div>
                            <div class=""DIVBoxContent"">
                                <div>", Title);
            writer.WriteLine(@"<ul class=""ULLineHeight150"">");
            foreach (ITreeNode node in DataSource.Nodes)
            {
                // Write begin LI
                writer.Write(@"<li class=""{0}""><a class=""title"" href=""javascript:void(0);"" title=""{1}"" >{1}</a>", node.HasNodes ? "LISquare" : "LIArrow", node.Name);
                
                // Write children
                if (node.HasNodes)
                    RenderNodes(writer, node);

                // Write end LI
                writer.WriteLine("</li>");
            }
            writer.WriteLine("</ul>");

            writer.WriteLine(@"</div>
                        </div>
                    </div>");
        }
        /// <summary>
        /// Render TreeNode
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="parent"></param>
        private void RenderNodes(Asp.UI.HtmlTextWriter writer, ITreeNode parent)
        {
            writer.WriteLine("<ul>");
            foreach (ITreeNode node in parent.Nodes)
            {
                // Write begin LI
                writer.Write(@"<li class=""{0}""><a class=""title"" href=""javascript:void(0);"" title=""{1}"" >{1}</a>", node.HasNodes ? "LISquare" : "LIArrow", node.Name);

                // Write children
                if (node.HasNodes)
                    RenderNodes(writer, node);

                // Write end LI
                writer.WriteLine("</li>");
            }
            writer.WriteLine("</ul>");
        }

        #region IPostBackDataHandler Members

        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            return false;
        }

        public void RaisePostDataChangedEvent()
        {
        }

        #endregion

        #region IPostBackEventHandler Members

        public void RaisePostBackEvent(string eventArgument)
        {
            
        }

        #endregion
    }
}
